/*
 * Copyright (C) 2022 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

import QtQuick 2.12
import org.ukui.tablet 1.0
import "../createUninstallDialog.js" as UninstallJs

AppIcon {
    id: item
    iconWidth: iconSize
    visible: modelData.type !== LauncherItem.Placeholder
    disableDelete: !modelManager.groupEditable(tabletRoot.groupId)
    tabletAppIconDropShadowVisible: true
    eduAppIconOpacityMaskVisible: false
    deleteIconVisible: modelData.uninstallable
    deleteIconLeftMargin: (-deleteIconWidth/2) * Style.tabletScaleFactor
    deleteIconTopMargin: (-deleteIconWidth/4) * Style.tabletScaleFactor
    deleteIconSource: "qrc:/ui/images/desktop-uninstallbtn.png"
    deleteIconSourceOnPressed: "qrc:/ui/images/desktop-uninstallbtn.png"
    deleteIconWidth: 30 * Style.tabletScaleFactor
    opacity: {
        if (modelData.type === LauncherItem.AppIcon) {
            if (listView.moving || dragMousearea.Drag.active) {
                return 1
            }
            if (dragMousearea.pressed && !dragMousearea.Drag.active) {
                return 0.5
            }
        }
        return 1
    }
    appIconSource: {
        switch (modelData.type) {
        case LauncherItem.AppIcon: {
            return "image://icons/" + modelData.icon
        }
        case LauncherItem.AddIcon:
            if (dragMousearea.pressed) {
                return "qrc:/ui/images/list-add-click.svg";
            } else {
                return "qrc:/ui/images/list-add-default.svg";
            }
        default: return ""
        }

    }
    iconState: modelData.state
    anchors {
        horizontalCenter: parent.horizontalCenter;
        verticalCenter: parent.verticalCenter
    }
    onDeleteIconClicked: {
        appIcon.visible = true
        UninstallJs.createUnInstallDialog(tabletRoot, modelData.desktopFile, modelData.name, modelData.icon)
        textPromptBox = model.modelData.name
        iconPromptBox = modelData.icon
    }
    onIconStateChanged: {
        if (modelData.type === LauncherItem.AddIcon) {
            if (iconState === LauncherItem.Normal) {
                item.visible = true
                itemName.visible = true
                dragMousearea.enabled = true
            } else {
                item.visible = false
                itemName.visible = false
                dragMousearea.enabled = false
            }
        }
    }
}
